Getting Started with PDF Viewer Control for .NET Windows Forms and WPF

 

Overview

 

The EVO PDF Viewer Control for .NET can be used in a Windows Forms and WPF applications to display a PDF document inside a form and allows the application users to visualize, browse, print and protect the PDF documents. The viewer requires the free Adobe Reader to be installed on the client computer in order to work properly.

The EVO PDF Viewer Control for .NET Windows Forms is delivered as a single assembly EvoPdfViewerWinForms.dll. The main class defined by this assembly is the PdfViewer class.

 

Adding the PDF Viewer Control for Windows Forms to the Visual Studio Toolbox

 
First of all you have to add the control from EvoPdfViewerWinForms.dll to the Visual Studio Toolbox. To do this, while you are in the design mode of a form, right click on the Toolbox panel and select 'Choose Items'. Browse to the EvoPdfViewerWinForms.dll and the PdfViewer control should have been added in the Toolbox panel. To use the control in a form of your Windows Forms application you simply drag and drop the PdfViewer control from the toolbox on the web page like you would do with any other server control. At this moment you are ready to use the control and display PDF documents inside your form.
 

Loading a PDF File

 
A PDF file can loaded into the PDF Viewer control using the LoadFile() method of the PdfViewer class. Here is a simple sample code to load the PDF file in the viewer in Load event handler of the parent form:
 
1:         private void MainForm_Load(object sender, EventArgs e)
2:         {
3:             string demoFilePath = System.IO.Path.Combine(Application.StartupPath, "evopfdf.pdf");
4:             pdfViewer1.LicenseKey = 
                        "WxrvvaNw5mDcoQ30mCbmRsrypMqSDHC2y4FhJojo+g+XWYQGh3F7cx6nZbdUAwgH";
5:             if (System.IO.File.Exists(demoFilePath))
6:             {
7:                 pdfViewer1.LoadFile(demoFilePath);
8:             }
9:         }
 
After you have loaded the PDF file in the viewer you can programmatically navigate to a page of the document, print the PDF document with or without the print dialog, set the zoom percentage, show or hide the viewer toolbars, set security options and and advanced viewer preferences. A full description of all the viewer methods can be found in the API Reference document PdfViewerWinNet.chm.
 

Loading a PDF Stream

 
A PDF stream can loaded into the PDF Viewer control using the LoadStream() method of the PdfViewer class. Here is a simple sample code to load the PDF file in the viewer in Load event handler of the parent form:
 
        private void MainForm_Load(object sender, EventArgs e)
        {
            string demoFilePath = System.IO.Path.Combine(Application.StartupPath, "evopdf.pdf");
            if (System.IO.File.Exists(demoFilePath))
            {
                FileStream fs = File.Open(demoFilePath, FileMode.Open);
                pdfViewer1.LoadStream(fs);
                fs.Close();
            }
        }
 
After you have loaded the PDFstream in the viewer you can programmatically navigate to a page of the document, print the PDF document with or without the print dialog, set the zoom percentage, show or hide the viewer toolbars, set security options and and advanced viewer preferences. A full description of all the viewer methods can be found in the API Reference document PdfViewerWinNet.chm.
 

Running the Samples

 
The samples are located in the Samples folder. To run the samples, open the projects with the Microsoft Visual Studio 2010 or Visual Studio 2012 and run them from there.
 

Using the PDF Viewer Control in 64-bit Environments

 
As mentioned in the beginning of this document, the PDF Viewer Control for .NET requires that the free Adobe Reader to be installed on the client computer in order to work properly. Because there is not a native version of Adobe Reader for 64-bit operating systems, the application using the PDF Viewer control must be built for x86 target instead of Any CPU in order to use the correct Adobe Reader interface. The target platform can be selected from Visual Studio Build Configuration Manager.
 

Using the PDF Viewer in WPF Applications

 
The PDF Viewer for .NET can be used in WPF applications. In order to use the viewer in a WPF application you first have to add a reference to the EvoPdfViewerWinForms.dll assembly in your WPF application. Then add a WindowsFormsHost control to the WPF window and add the PDF Viewer control to this container. We provide the WPF_PdfViewerWinNetDemo sample to demonstrate this approach.

In the form where you intend to add the viewer you have to insert the pdf viewer namespace in window definition like in the highlighted line from the code below:
<Window x:Class="WPF_PdfViewerWinNetDemo.MainForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WinFormsPdfViewer="clr-namespace:PdfViewer4WinNet;assembly=PdfViewerWinNet"
        Title="PDF Viewer Control
        for .NET 2.0 Demo" Height="728" Width="760" Loaded="Window_Loaded" 
    Icon="/WPF_PdfViewerWinNetDemo;component/wsico.ico">
The next step is to add to the WPF application form a WindowsFormsHost control containing the PDF Viewer control:
<Grid Height="Auto" Background="GhostWhite">
        <WindowsFormsHost Margin="0,0,0,53" Name="pdfViewerWinFormsHost">
            <WinFormsPdfViewer:PdfViewer x:Name="pdfViewerWinFormsControl" />
        </WindowsFormsHost>
</Grid>
The pdfViewerWinFormsControl object will be available now in the WPF form code behind and can be used to load the document, set the viewer preferences, navigate print and protect the displayed PDF document.
 

Licensing

 
The LicenseKey property of the PdfViewer class should be set with the string you received after the purchase of the product.
 

Links

EVO PDF Viewer for .NET Windows Forms Home Page